package com.qianclass.talklive.app.view.meunView;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import com.qianclass.talklive.R;

import java.util.ArrayList;

/**
 * 悬浮菜单、动画
 */
public class PromotedActionsLibrary {

	public Context context;

	public FrameLayout frameLayout;

	public ImageButton mainImageButton;

	public ImageButton meunEditButton;

	public ImageButton menuCameraButton;

	public ImageButton menuLockButton;

	public ImageButton menuLimitAllButton;

	public ImageButton menuFileButton;

	public ImageButton menuClearButton;

	public ImageButton menuRevokeButton;

	public ImageButton menuReveserRevocationButton;

	public RotateAnimation rotateOpenAnimation;

	public RotateAnimation rotateCloseAnimation;

	public ArrayList<ImageButton> promotedActions;

	public ObjectAnimator objectAnimator[];

	private int px;

	private static final int ANIMATION_TIME = 80;

	public boolean isMenuOpened;

	public void setup(Context activityContext, FrameLayout layout) {
		context = activityContext;
		promotedActions = new ArrayList<ImageButton>();
		frameLayout = layout;
		px = (int) context.getResources().getDimension(R.dimen.dim42dp);
		openRotation();
		closeRotation();
	}

	/**
	 * 
	 * Description 添加默认显示图标
	 * @param drawable
	 * @return
	 * @see [class/class#field/class#method]
	 */
	public ImageButton addMainItem(Drawable drawable) {

		ImageButton button = (ImageButton) LayoutInflater.from(context)
				.inflate(R.layout.main_promoted_action_button, frameLayout,
						false);

		button.setImageDrawable(drawable);

		button.setSelected(false);

		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {

				if (isMenuOpened) {
					closePromotedActions().start();
					isMenuOpened = false;
				} else {
					isMenuOpened = true;
					openPromotedActions().start();
				}
			}
		});

		frameLayout.addView(button);

		mainImageButton = button;

		return button;
	}

	/**
	 * 
	 * Description 添加需要弹出的按钮图标 ,画笔
	 * @param drawable
	 * @param onClickListener
	 *  @param tag
	 * @see [class/class#field/class#method]
	 */
	public void addItemEdit(Drawable drawable, View.OnClickListener onClickListener , String tag) {

		ImageButton button = (ImageButton) LayoutInflater.from(context)
				.inflate(R.layout.main_promoted_action_button, frameLayout, false);

		button.setImageDrawable(drawable);

		button.setOnClickListener(onClickListener);

		button.setTag(tag);

		button.setSelected(false);

		meunEditButton = button;

		promotedActions.add(button);

		frameLayout.addView(button);

		return;
	}


	/**
	 *
	 * Description 添加需要弹出的按钮图标 ,相机
	 * @param drawable
	 * @param onClickListener
	 *  @param tag
	 * @see [class/class#field/class#method]
	 */
	public void addItem(Drawable drawable, View.OnClickListener onClickListener , String tag) {

		ImageButton button = (ImageButton) LayoutInflater.from(context)
				.inflate(R.layout.main_promoted_action_button, frameLayout, false);

		button.setImageDrawable(drawable);

		button.setOnClickListener(onClickListener);

		button.setTag(tag);

		button.setSelected(false);

		menuCameraButton = button;

		promotedActions.add(button);

		frameLayout.addView(button);

		return;
	}

	/**
	 *
	 * Description 添加需要弹出的按钮图标 ,解锁
	 * @param drawable
	 * @param onClickListener
	 *  @param tag
	 * @see [class/class#field/class#method]
	 */
	public void addItemLock(Drawable drawable, View.OnClickListener onClickListener , String tag) {

		ImageButton button = (ImageButton) LayoutInflater.from(context)
														 .inflate(R.layout.main_promoted_action_button, frameLayout, false);

		button.setImageDrawable(drawable);

		button.setOnClickListener(onClickListener);

		button.setTag(tag);

		button.setSelected(false);

		menuLockButton = button;

		promotedActions.add(button);

		frameLayout.addView(button);

		return;
	}

	/**
	 *
	 * Description 添加需要弹出的按钮图标 ,禁言
	 * @param drawable
	 * @param onClickListener
	 *  @param tag
	 * @see [class/class#field/class#method]
	 */
	public void addItemLimitAll(Drawable drawable, View.OnClickListener onClickListener , String tag) {

		ImageButton button = (ImageButton) LayoutInflater.from(context)
														 .inflate(R.layout.main_promoted_action_button, frameLayout, false);

		button.setImageDrawable(drawable);

		button.setOnClickListener(onClickListener);

		button.setTag(tag);

		button.setSelected(false);

		menuLimitAllButton = button;

		promotedActions.add(button);

		frameLayout.addView(button);

		return;
	}

	/**
	 *
	 * Description 添加需要弹出的按钮图标 ,文件
	 * @param drawable
	 * @param onClickListener
	 *  @param tag
	 * @see [class/class#field/class#method]
	 */
	public void addItemFile(Drawable drawable, View.OnClickListener onClickListener , String tag) {

		ImageButton button = (ImageButton) LayoutInflater.from(context)
														 .inflate(R.layout.main_promoted_action_button, frameLayout, false);

		button.setImageDrawable(drawable);

		button.setOnClickListener(onClickListener);

		button.setTag(tag);

		button.setSelected(false);

		menuFileButton = button;

		promotedActions.add(button);

		frameLayout.addView(button);

		return;
	}

	/**
	 *
	 * Description 添加需要弹出的按钮图标 ,清空
	 * @param drawable
	 * @param onClickListener
	 *  @param tag
	 * @see [class/class#field/class#method]
	 */
	public void addItemClear(Drawable drawable, View.OnClickListener onClickListener , String tag) {

		ImageButton button = (ImageButton) LayoutInflater.from(context)
														 .inflate(R.layout.main_promoted_action_button, frameLayout, false);

		button.setImageDrawable(drawable);

		button.setOnClickListener(onClickListener);

		button.setTag(tag);

		button.setSelected(false);

		menuClearButton = button;

		promotedActions.add(button);

		frameLayout.addView(button);

		return;
	}

	/**
	 *
	 * Description 添加需要弹出的按钮图标 ,撤销
	 * @param drawable
	 * @param onClickListener
	 *  @param tag
	 * @see [class/class#field/class#method]
	 */
	public void addItemRevoke(Drawable drawable, View.OnClickListener onClickListener , String tag) {

		ImageButton button = (ImageButton) LayoutInflater.from(context)
														 .inflate(R.layout.main_promoted_action_button, frameLayout, false);

		button.setImageDrawable(drawable);

		button.setOnClickListener(onClickListener);

		button.setTag(tag);

		button.setSelected(false);

		menuRevokeButton = button;

		promotedActions.add(button);

		frameLayout.addView(button);

		return;
	}

	/**
	 *
	 * Description 添加需要弹出的按钮图标 ,反撤销
	 * @param drawable
	 * @param onClickListener
	 *  @param tag
	 * @see [class/class#field/class#method]
	 */
	public void addItemReveserRevocation(Drawable drawable, View.OnClickListener onClickListener , String tag) {

		ImageButton button = (ImageButton) LayoutInflater.from(context)
														 .inflate(R.layout.main_promoted_action_button, frameLayout, false);

		button.setImageDrawable(drawable);

		button.setOnClickListener(onClickListener);

		button.setTag(tag);

		button.setSelected(false);

		menuReveserRevocationButton = button;

		promotedActions.add(button);

		frameLayout.addView(button);

		return;
	}

	/**
	 * 
	 * Description 判断选择的状态 
	 * @return
	 * @see [class/class#field/class#method]
	 */
	public boolean isSelectTag(){
		return isMenuOpened;
	}

	/**
	 * Set close animation for promoted actions
	 */
	public AnimatorSet closePromotedActions() {
		isMenuOpened = false;
		if (objectAnimator == null) {
			objectAnimatorSetup();
		}

		AnimatorSet animation = new AnimatorSet();

		for (int i = 0; i < promotedActions.size(); i++) {

			objectAnimator[i] = setCloseAnimation(promotedActions.get(i), i);
		}

		if (objectAnimator.length == 0) {
			objectAnimator = null;
		}

		animation.playTogether(objectAnimator);
		animation.addListener(new Animator.AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animator) {
				mainImageButton.startAnimation(rotateCloseAnimation);
				mainImageButton.setClickable(false);
			}

			@Override
			public void onAnimationEnd(Animator animator) {
				mainImageButton.setClickable(true);
				hidePromotedActions();
			}

			@Override
			public void onAnimationCancel(Animator animator) {
				mainImageButton.setClickable(true);
			}

			@Override
			public void onAnimationRepeat(Animator animator) {
			}
		});

		return animation;
	}

	public AnimatorSet openPromotedActions() {

		if (objectAnimator == null) {
			objectAnimatorSetup();
		}

		AnimatorSet animation = new AnimatorSet();

		for (int i = 0; i < promotedActions.size(); i++) {

			objectAnimator[i] = setOpenAnimation(promotedActions.get(i), i);
		}

		if (objectAnimator.length == 0) {
			objectAnimator = null;
		}

		animation.playTogether(objectAnimator);
		animation.addListener(new Animator.AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animator) {
				mainImageButton.startAnimation(rotateOpenAnimation);
				mainImageButton.setClickable(false);
				showPromotedActions();
			}

			@Override
			public void onAnimationEnd(Animator animator) {
				mainImageButton.setClickable(true);
			}

			@Override
			public void onAnimationCancel(Animator animator) {
				mainImageButton.setClickable(true);
			}

			@Override
			public void onAnimationRepeat(Animator animator) {
			}
		});

		return animation;
	}

	private void objectAnimatorSetup() {

		objectAnimator = new ObjectAnimator[promotedActions.size()];
	}

	/**
	 * 为按钮设置关闭动画
	 *
	 * @param promotedAction
	 * @param position
	 * @return objectAnimator
	 */
	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	private ObjectAnimator setCloseAnimation(ImageButton promotedAction,
											 int position) {

		ObjectAnimator objectAnimator;

		if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {

			objectAnimator = ObjectAnimator.ofFloat(promotedAction,
					View.TRANSLATION_X, px
							* (promotedActions.size() - position), 0f);
			objectAnimator.setRepeatCount(0);
			objectAnimator.setDuration(ANIMATION_TIME
					* (promotedActions.size() - position));

		} else {

			objectAnimator = ObjectAnimator.ofFloat(promotedAction,
					View.TRANSLATION_Y, -px
							* (promotedActions.size() - position), 0f);
			objectAnimator.setRepeatCount(0);
			objectAnimator.setDuration(ANIMATION_TIME
					* (promotedActions.size() - position));
		}

		return objectAnimator;
	}

	/**
	 * 为按钮设置打开动画
	 *
	 * @param promotedAction
	 * @param position
	 * @return objectAnimator
	 */
	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	private ObjectAnimator setOpenAnimation(ImageButton promotedAction,
											int position) {

		ObjectAnimator objectAnimator;

		if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {

			objectAnimator = ObjectAnimator.ofFloat(promotedAction,
					View.TRANSLATION_X, 0f, px
							* (promotedActions.size() - position));
			objectAnimator.setRepeatCount(0);
			objectAnimator.setDuration(ANIMATION_TIME
					* (promotedActions.size() - position));

		} else {
			objectAnimator = ObjectAnimator.ofFloat(promotedAction,
					View.TRANSLATION_Y, 0f, -px
							* (promotedActions.size() - position));
			objectAnimator.setRepeatCount(0);
			objectAnimator.setDuration(ANIMATION_TIME
					* (promotedActions.size() - position));
		}

		return objectAnimator;
	}

	private void hidePromotedActions() {

		for (int i = 0; i < promotedActions.size(); i++) {
			promotedActions.get(i).setVisibility(View.GONE);
		}
	}

	private void showPromotedActions() {

		for (int i = 0; i < promotedActions.size(); i++) {
			promotedActions.get(i).setVisibility(View.VISIBLE);
		}
	}

	private void openRotation() {

		rotateOpenAnimation = new RotateAnimation(0, 45,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotateOpenAnimation.setFillAfter(true);
		rotateOpenAnimation.setFillEnabled(true);
		rotateOpenAnimation.setDuration(ANIMATION_TIME);
	}

	public void closeRotation() {

		rotateCloseAnimation = new RotateAnimation(45, 0,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotateCloseAnimation.setFillAfter(true);
		rotateCloseAnimation.setFillEnabled(true);
		rotateCloseAnimation.setDuration(ANIMATION_TIME);
	}

}
